package org.footito.android.adapter;

import java.util.ArrayList;

import org.footito.android.R;
import org.footito.android.data.Comment;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CommentsBaseAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ArrayList<Comment> list;

	public CommentsBaseAdapter(Context context, ArrayList<Comment> commentsList) {
		inflater = LayoutInflater.from(context);
		this.list = commentsList;
	}

	@Override
	public int getCount() {
		if (list.size() < 1)
			return 1;
		else
			return list.size();
	}

	@Override
	public Object getItem(int location) {
		if (list.size() < 1)
			return null;
		else
			return list.get(location);
	}

	@Override
	public long getItemId(int location) {
		return location;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.row_comment, null);
		}
		TextView comment = (TextView) convertView.findViewById(R.id.TV_Comment);
		ImageView flag = (ImageView) convertView.findViewById(R.id.IV_Flag);

		if (list.size() < 1) {
			comment.setText(parent.getContext().getResources().getString(R.string.no_comment));
			flag.setVisibility(View.GONE);
		} else {
			Comment current = list.get(position);
			comment.setText(Html.fromHtml("<b>" + current.getTeam().getName() + "</b> : "
					+ current.getContent()));
			Bitmap bitmap = current.getTeam().getPicture(parent.getContext());
			flag.setImageBitmap(bitmap);
			flag.setVisibility(View.VISIBLE);
		}

		return convertView;
	}

}
